package com.yyn.pigeon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.pigeon.config.exceptionhandler.CustomException;
import com.yyn.pigeon.entity.Goods;
import com.yyn.pigeon.entity.dto.GoodsCategory;
import com.yyn.pigeon.mapper.GoodsMapper;
import com.yyn.pigeon.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2023-03-15
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
@Autowired
private GoodsMapper goodsMapper;

    @Override
    public List<Goods> getAll(String keyWord, String category) {
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsLambdaQueryWrapper.orderByDesc();
        if(keyWord != null){
            LambdaQueryWrapper<Goods> nameCon = goodsLambdaQueryWrapper.eq(Goods::getName, keyWord);
            if(nameCon ==null ){
                goodsLambdaQueryWrapper.eq(Goods::getDescription,keyWord);
            }else{
                throw new CustomException(20001,"没有查询出结果");
            }
        }
        if(category !=null){//根据分类查
            List<Goods> goodsByCategory = goodsMapper.getGoodsByCategory(category);
            return goodsByCategory;
        }
        List<Goods> goods = goodsMapper.selectList(goodsLambdaQueryWrapper);
        return goods;
    }

    @Override
    public Goods getGoodById(String id) {
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsLambdaQueryWrapper.eq(Goods::getId,id);
        Goods good = goodsMapper.selectOne(goodsLambdaQueryWrapper);
        return good;
    }
}
